package com.example.wengwyue.patientapplication.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wengwyue.patientapplication.R;
import com.example.wengwyue.patientapplication.model.AccountInfo;
import com.example.wengwyue.patientapplication.model.CaseBaseInfo;
import com.example.wengwyue.patientapplication.model.PrescribedInfo;
import com.example.wengwyue.patientapplication.model.result.CaseBaseInfoResult;
import com.example.wengwyue.patientapplication.network.ApiManager;
import com.example.wengwyue.patientapplication.utils.DateUtils;
import com.hospital.common.api.DataCallback;
import com.hospital.common.api.model.Error;

import java.util.List;

/**
 * 作者： wengwyue   时间： 16/8/10.
 * 功能：
 * 修改：
 */
public class CurrentPatientFragment extends BaseVaryViewFragment {
    /*基本信息view*/
    private TextView number;
    private TextView date;
    private TextView bedNumber;
    private TextView doctorName;
    private TextView nurseName;

    /*支付信息*/
    private TextView payCount;
    private TextView costCount;
    private TextView remainCount;

    private LinearLayout secularInstructiionLayout; // 长期医嘱
    private LinearLayout tempInstructiionLayout;
    private View mScrollContentView;


    @Override
    protected void initView(View rootView) {
        this.mScrollContentView = rootView;
        initBaseInfo(rootView);
        initAccountInfo(rootView);
        super.initView(rootView);
    }

    @Override
    public View provideDataView() {
        return mScrollContentView;
    }

    private void initBaseInfo(View rootView) {
        secularInstructiionLayout = (LinearLayout) rootView.findViewById(R.id.secular_instructions_layout);
        tempInstructiionLayout = (LinearLayout) rootView.findViewById(R.id.temp_instructions_layout);
        number = (TextView) rootView.findViewById(R.id.number_text);
        date = (TextView) rootView.findViewById(R.id.date_text);
        bedNumber = (TextView) rootView.findViewById(R.id.bed_number_text);
        doctorName = (TextView) rootView.findViewById(R.id.excute_doctor_text);
        nurseName = (TextView) rootView.findViewById(R.id.excute_nurse_text);

    }
    private void initAccountInfo(View rootView) {
        payCount = (TextView) rootView.findViewById(R.id.pay_count_text);
        costCount = (TextView) rootView.findViewById(R.id.cost_count_text);
        remainCount = (TextView) rootView.findViewById(R.id.remain_count_text);
    }
    @Override
    protected void initListener() {

    }

    @Override
    protected void initData(View rootView, Bundle savedInstanceState) {
        getCaseInfo();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getCaseInfo();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected int provideLayoutResId() {
        return R.layout.fragment_current_patient;
    }

    private void getCaseInfo(){
        mVaryViewHelper.showLoadingView();
        ApiManager.getCaseInfo(new DataCallback() {
            @Override
            public void onSuccess(Object data) {
                mVaryViewHelper.showDataView();
                if (data != null) {
                    CaseBaseInfoResult info = (CaseBaseInfoResult) data;
                    setBaseInfo(info.caseBaseInfo);
                    setAccountInfo(info.accountInfo);
                    setSecularInstructiionLayout(info.sos);
                    setTempInstructiionLayout(info.prnn);
                }else {
                    mVaryViewHelper.showEmptyView();
                }
            }

            @Override
            public void onFailed(Error error) {
                mVaryViewHelper.showEmptyView();
                Toast.makeText(getActivity(),error.des,Toast.LENGTH_SHORT).show();
            }

        });
    }

    @Override
    protected void onReLoad() {
        getCaseInfo();
    }

    private void setBaseInfo(CaseBaseInfo info) {
        if (info == null) {
            return ;
        }
        number.setText(String.valueOf(info.caseID));
        date.setText(String.valueOf(DateUtils.timeToDate(info.inTime)));
        bedNumber.setText(info.bedName);
        doctorName.setText(info.doctorName);
        nurseName.setText(info.nurseName);
    }
    private void setAccountInfo(AccountInfo info) {
        payCount.setText(String.format(getResources().getString(R.string.money_format),info.getBalance()));
        costCount.setText(String.format(getResources().getString(R.string.money_format),info.getPay()));
        remainCount.setText(String.format(getResources().getString(R.string.money_format),info.getRemain()));
    }
    private void setSecularInstructiionLayout(List<PrescribedInfo> datas) {
//        secularInstructiionLayout.removeAllViews();
        if (datas == null) {
            return;
        }
        for (PrescribedInfo item : datas) {
            if (getActivity() != null) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_instructions_layout, null);
                ViewGroup.LayoutParams p = view.getLayoutParams();
                if (p == null) {
                    p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                }
                view.setLayoutParams(p);
                TextView title = (TextView) view.findViewById(R.id.instructions_text);
                TextView cost = (TextView) view.findViewById(R.id.cost_text);
                title.setText(item.name);
                cost.setText(String.format(getActivity().getResources().getString(R.string.money_format),item.pay));
                secularInstructiionLayout.addView(view);
            }
        }
    }
    private void setTempInstructiionLayout(List<PrescribedInfo> datas) {
//        secularInstructiionLayout.removeAllViews();
        if (datas == null) {
            return;
        }
        for (PrescribedInfo item : datas) {
            if (getActivity() != null) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_instructions_layout, null);
                ViewGroup.LayoutParams p = view.getLayoutParams();
                if (p == null) {
                    p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                }
                view.setLayoutParams(p);
                TextView title = (TextView) view.findViewById(R.id.instructions_text);
                TextView cost = (TextView) view.findViewById(R.id.cost_text);
                title.setText(item.name);
                cost.setText(String.format(getActivity().getResources().getString(R.string.money_format),item.pay));
                tempInstructiionLayout.addView(view);
            }
        }
    }


}
